from app.exts import database
from datetime import datetime


class User(database.Model):
    """
    用户信息表，用于存储用户信息（不存储用户密码）
    user_id:用户id，主键，自增长
    username:用户名，默认设置为邮箱
    email:用户邮箱，不能为空，唯一
    sex:性别，默认为2（未设置）
    age:年龄，默认为0
    avatar_url:用户头像链接
    is_admin:是否为管理员，默认为0
    """
    __tablename__ = "user_info"
    user_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    username = database.Column(database.String(20), nullable=False)
    email = database.Column(database.String(255), nullable=False, unique=True)
    sex = database.Column(database.Integer, default=2)
    age = database.Column(database.Integer, default=0)
    avatar_url = database.Column(database.Text,
                                 default="https://tse2-mm.cn.bing.net/th/id/OIP-C.jHUH4s7TQ4…ozuJgHaHa?w=188&h=188&c=7&r=0&o=5&dpr=1.3&pid=1.7",
                                 nullable=False)
    is_admin = database.Column(database.Integer, default=0, nullable=False)


class Login(database.Model):
    """
    登陆信息表，用于验证登录信息，只存储账号密码
    id:用户id，主键
    email:用户邮箱
    password:密码，8-16位
    """
    __tablename__ = "user_login"
    user_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    email = database.Column(database.String(255), nullable=False)
    password = database.Column(database.String(16), nullable=False)


class Book(database.Model):
    """
    图书信息表，用于存储图书信息
    book_id:图书id，主键
    book_name	varchar(50)	图书名称，不能为空
    author	varchar(50)	作者，默认为佚名
    text	text	图书简介，不超过50字，默认为暂无简介
    image_url	text	图书图片链接，默认为默认图片链接
    borrow_count	int	图书被借阅次数，默认为0
    current_number	int	现存数量，默认为0
    number	int	库存总数，默认为0
    category_id	int	图书所属大类别，外键
    product_id	int	图书所属小类别，外键

    """
    __tablename__ = "book_info"
    book_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    book_name = database.Column(database.String(50), nullable=False)
    author = database.Column(database.String(50), default="佚名")
    text = database.Column(database.Text, default="暂无简介", nullable=False)
    image_url = database.Column(database.Text,
                                default="https://tse2-mm.cn.bing.net/th/id/OIP-C.jHUH4s7TQ4…ozuJgHaHa?w=188&h=188&c=7&r=0&o=5&dpr=1.3&pid=1.7",
                                nullable=False)
    borrow_count = database.Column(database.Integer, default=0)
    current_number = database.Column(database.Integer, default=0)
    number = database.Column(database.Integer, default=0)
    category_id = database.Column(database.Integer, nullable=False)
    product_id = database.Column(database.Integer, nullable=False)


class Category(database.Model):
    """
    category_id	int	图书大类别id，主键
    category_name	varchar(20)	图书大类别名称，不超过20个字符，不能为空

    """
    __tablename__ = "book_category"
    category_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    category_name = database.Column(database.String(20), nullable=False)


class Product(database.Model):
    """
    product_id	int	图书小类别id，主键
    product_name	varchar(20)	图书小类别名称，不超过20个字符，不能为空
    category	int	图书大类别id，外键

    """
    __tablename__ = "book_product"
    product_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    product_name = database.Column(database.String(20), nullable=False)
    category_id = database.Column(database.Integer, nullable=False)


class Borrow(database.Model):
    """
    借阅信息表，根据用户id，在图书借阅表查询属于该用户的所有借阅书籍信息
    borrow_id   借阅id，主键
    user_id	用户id，外键
    book_id	书籍id，外键
    borrow_time	date	借出日期
    return_time	date	归还日期，若为null，则说明这次借阅还未完成
    book_status	int	书籍状态，默认为0（借阅中）
    """
    __tablename__ = "borrow_info"
    borrow_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    borrow_time = database.Column(database.Date, default=datetime.now(), nullable=False)
    return_time = database.Column(database.Date)
    book_status = database.Column(database.Integer, default=0)
    user_id = database.Column(database.Integer, nullable=False)
    book_id = database.Column(database.Integer, nullable=False)


class Collect(database.Model):
    """
    用户收藏表，存储所有用户的收藏表单
    collection_id:收藏id，主键
    user_id:用户id，外键
    book_id:书籍id，外键
    """
    __tablename__ = "user_collect"
    collection_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    user_id = database.Column(database.Integer, nullable=False)
    book_id = database.Column(database.Integer, nullable=False)


class Feedback(database.Model):
    """
    le
    feedback_id	int	反馈id，主键
    user_id	int	用户id，外键
    message	text	信息
    """
    __tablename__ = "user_feedback"
    feedback_id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    message = database.Column(database.Text, nullable=False)
    user_id = database.Column(database.Integer, nullable=False)


class EmailCaptchaModel(database.Model):
    __tablename__ = "email_captcha"
    id = database.Column(database.Integer, primary_key=True, autoincrement=True)
    email = database.Column(database.String(255), nullable=False)
    captcha = database.Column(database.String(4), nullable=False)
